<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>通过变量控制元素得到显示或隐藏</title>
</head>
<body>
<div id="change" class="change">
    <h1 data-show="state" style="background-color: red">Hello, World!</h1>
    <h1 data-show="!state" style="background-color: green">Hello, World!</h1>
</div>

<script>
    let state = true
    // step1 获取目标元素
    let elements = document.querySelectorAll("#change > h1");
    for (let i = 0; i < elements.length; i++) {
        // console.log(elements[i]);
        // 获取属性的值
        let attribute = eval(elements[i].getAttribute("data-show")) ;
        // console.log(attribute);

        // 根据属性的值控制显示/隐藏
        if (attribute === state) {
            // elements[i].style = "display: none"
            elements[i].style.display = "none"
        } else {

        }
    }
</script>
</body>
</html>